package beans;

import entities.JeuVideo;
import entities.LigneDePanier;
import entities.Panier;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class CartFacade extends AbstractFacade<Panier> {

    @PersistenceContext(unitName = "ProjetAppliWebPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public CartFacade() {
        super(Panier.class);
    }

    public Panier create() {
        Panier cart = new Panier();
        create(cart);
        return cart;
    }

    public void empty(Panier cart) {
        cart.getLigneDePanier().clear();
        edit(cart);
    }

    public void addVideoGame(Panier cart, JeuVideo videoGame) {
        LigneDePanier cartLine = new LigneDePanier(videoGame);
        cart.getLigneDePanier().add(cartLine);
        edit(cart);
    }

    public void removeVideoGame(Panier cart, JeuVideo videoGame) {
        LigneDePanier cartLine = null;
        for (LigneDePanier cartLine2 : cart.getLigneDePanier()) {
            if (cartLine2.getJeuVideo().getId() == videoGame.getId()) {
                cartLine = cartLine2;
            }
        }
        cart.getLigneDePanier().remove(cartLine);
        edit(cart);
    }
}
